package com.ym.controller.soso;

import com.ym.common.constant.Constants;
import com.ym.core.annotation.Log;
import com.ym.core.config.ConfigCenterProperties;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.es.domain.bo.GlobalSearchParam;
import com.ym.core.es.domain.bo.GlobalSearchResult;
import com.ym.core.es.domain.index.GlobalSearchWordIndex;
import com.ym.soso.service.ISearchSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * 首页
 *
 * @author: 李涛
 * @version: 2019年11月18日 10:51
 */
@Api(tags = "soso-首页")
@RestController
@RequestMapping(value = "/soso/home", headers = Constants.Api.VERSION)
@Validated
public class HomeController extends BaseController {

	@Autowired
	private ISearchSV searchSV;

	@Autowired
	private ConfigCenterProperties configCenterProperties;


	@Log(dataBase = false, printBody = true)
	@ApiOperation("引导搜索")
	@GetMapping(value = "/global/search/prev")
	public APIResponse<List<GlobalSearchWordIndex>> globalSearchPrev(String prov, String city, String area, String keyword) throws Exception {
		List<GlobalSearchWordIndex> globalSearchPrevResult = searchSV.globalSearchPrev(prov, city, area, keyword);
		return successData(globalSearchPrevResult);
	}

	@Log(dataBase = false, printBody = true)
	@ApiOperation("全局搜索")
	@GetMapping(value = "/global/search")
	public APIResponse<GlobalSearchResult> globalSearch(
			@Valid GlobalSearchParam param
	) {
		GlobalSearchResult globalSearchResult = searchSV.globalSearch(param);
		return successData(globalSearchResult);
	}

	@Log(dataBase = false, printBody = true)
	@ApiOperation("热门搜索数据")
	@GetMapping(value = "/global/search/hot")
	public APIResponse<Set<String>> globalSearchHot(@RequestParam(required = false, defaultValue = "5") int limit) {
		Set<String> keywords = searchSV.getHotKeywords(limit);
		return successData(keywords);
	}
}
